package org.jiuling.network.tools.http.request;

import org.jiuling.local.tools.exception.LocalToolsException;
import org.jiuling.local.tools.util.RegularUtil;
import org.jiuling.local.tools.verify.ObjectVerify;
import org.jiuling.network.tools.http.request.enums.RequestMethodEnum;
import org.jiuling.network.tools.http.request.exception.RequestException;
import org.jiuling.network.tools.http.request.factory.HttpRequestFactory;
import org.jiuling.network.tools.http.request.factory.impl.HttpRequestImpl;
import org.jiuling.network.tools.http.request.factory.impl.HttpsRequestImpl;
import org.jiuling.network.tools.http.request.model.HttpResponse;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * Http请求
 * @author 紫灵
 * @date 2022年02月25日 15时15分25秒
 */
public class HttpRequest {

    /** 请求地址 */
    private String path;

    /** http请求工厂 */
    private HttpRequestFactory httpRequestFactory;

    /**
     * 开启http请求工厂
     * @param path 请求路径
     * @return http请求工厂接口
     */
    public HttpRequestFactory openHttpRequestFactory(String path) throws RequestException {
        this.path = path;
        if(ObjectVerify.isNull(this.httpRequestFactory )){
            if(Pattern.matches("http://.*",path)){
                this.httpRequestFactory = new HttpRequestImpl();
            }else if(Pattern.matches("https://.*",path)){
                this.httpRequestFactory = new HttpsRequestImpl();
            }else {
                throw new RequestException("不支持该请求协议，请使用HTTP协议或HTTPS协议！");
            }
        }
        return this.httpRequestFactory;
    }


    /**
     * 变更请求地址
     * @param path 请求地址
     * @return http请求
     */
    public HttpRequest changePath(String path) throws RequestException {
        String regex = "(.*?)://";
        String ordProtocol = RegularUtil.getValue(regex,this.path).get(0);
        String newProtocol = RegularUtil.getValue(regex,path).get(0);
        if(Objects.equals(ordProtocol,newProtocol)){
            this.path = path;
        }else {
            throw new RequestException("原协议与新协议不同，无法变更！");
        }
        return this;
    }

    /**
     * 发送请求
     * @return http相应
     */
    public HttpResponse send() throws IOException, LocalToolsException, RequestException, NoSuchAlgorithmException, KeyManagementException {
        return this.httpRequestFactory.send(this.path);
    }

    public static void main(String[] args) {
        String host = "https://finacityid.market.alicloudapi.com";
        String path = "/whapi/json/alicityweather/limit";
        String appcode = "b48eb46d87f645af81d11c9714beb7b7";

        try {
            HttpRequest httpRequest = new HttpRequest();
            HttpRequestFactory httpRequestFactory = httpRequest.openHttpRequestFactory("http://domaidetection.test.jiulingcloud.cn/");
//            HttpRequestHeader httpRequestHeader = new HttpRequestHeader()
//                    .add("Authorization","APPCODE "+appcode)
//                    .add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//            HttpRequestParam httpRequestBody = new HttpRequestParam().add("cityId", "2635").add("token", "008d2ad9197090c5dddc76f583616606");
//            httpRequestFactory.setRequestParam(httpRequestBody);
//            httpRequestFactory.setRequestHeader(httpRequestHeader);
            httpRequestFactory.setRequestMethod(RequestMethodEnum.GET);
//            System.out.println(httpRequest.send().getBody());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
